//
//  QCStatusViewModel.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/4.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class QCStatusViewModel: NSObject {

    var homeModel:QCHomeModel?{
        didSet {
        membershipImage = dealMembershipImage(mbrank: homeModel?.user?.mbrank ?? 0)
         avatarImage = dealAvatarImage(verified: homeModel?.user?.verified ?? -1)
        
        sourceStr = dealSinaSource(source: homeModel?.source)
        }
    }
    
    var membershipImage:UIImage?
    var avatarImage:UIImage?
    var sourceStr:String?
    var sinaTimeStr: String?{
        return dealSinaTime(creatAT: homeModel?.created_at)
    }
}


extension QCStatusViewModel{
    
    func dealMembershipImage(mbrank: Int) -> UIImage? {
        if mbrank >= 1 && mbrank <= 6{
            return UIImage(named:"common_icon_membership_level\(mbrank)")
        }else {
            return UIImage(named:"common_icon_membership")
        }
        
        
    }
    
    func dealAvatarImage(verified: Int) -> UIImage? {
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    func dealSinaSource(source:String?)->String{
        
        guard let s = source,s.contains("\">") else {
            return "来自未知的世界"
        }
        //获取起始光标
        let start = s.range(of: "\">")!
        //获取结束光标
        let end = s.range(of: "</a")!
        //取到最终需要的结果
        let result = s.substring(with: start.upperBound..<end.lowerBound)
        return "来自:" + result
        
    }
    
    func dealSinaTime(creatAT:Date?) -> String?{
        
        guard let sinaDate = creatAT else {
            return nil
        }
        
        let df = DateFormatter()
        
        let isThisYear = sinaDateisThisYear(sinaDate: sinaDate)
        
        if isThisYear {
            
            let calendar = Calendar.current

            if calendar.isDateInToday(sinaDate) {
                let s  = abs(sinaDate.timeIntervalSinceNow)
               
                if s <= 60 {
                    return "刚刚"
                }else if s > 60 && s <= 3600 {
                    return "\(Int(s/60))分钟前"
                }else {
                    return "\(Int(s/(60*60)))小时前"

                }
                
            }else if calendar.isDateInYesterday(sinaDate){
                
                df.dateFormat = "昨天 HH:mm"
                return df.string(from: sinaDate)
                
            }else {
                df.dateFormat = "MM-dd HH-mm"
                return df.string(from: sinaDate)
            }
            
        }else {
            df.dateFormat = "yyyy-MM-dd HH:mm"
            return df.string(from: sinaDate)
        }
    }
    
    func sinaDateisThisYear(sinaDate:Date)->Bool{
        //获取当前日期
        let currentDate = Date()
        //时间格式化
        let df = DateFormatter()
        //指定时间格式
        df.dateFormat = "yyyy"
        //通过当前的日期得到日期年的字符串
        let currentStr = df.string(from: currentDate)
        //获取新浪微博的日期年的字符串
        let sinaStr = df.string(from: sinaDate)
        //返回是否相等
        return currentStr == sinaStr
    }
    
}
